﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CUiC3DSplitButton
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;


namespace DnProcessSimulateCommands;

public class CUiC3DSplitButton : System.Windows.Forms.Button
{
  private PushButtonState m_state;
  private const int m_pushButtonWidth = 14;
  private static int m_borderSize = SystemInformation.Border3DSize.Width * 2;
  private bool m_skipNextOpen;
  private Rectangle m_dropDownRectangle;
  private bool m_showSplit = true;
  private TextFormatFlags m_textFormatFlags;

  public CUiC3DSplitButton() => this.AutoSize = true;

  [DefaultValue(true)]
  public bool ShowSplit
  {
    set
    {
      if (value == this.m_showSplit)
        return;
      this.m_showSplit = value;
      this.Invalidate();
      if (this.Parent == null)
        return;
      this.Parent.PerformLayout();
    }
  }

  private PushButtonState State
  {
    get => this.m_state;
    set
    {
      if (this.m_state.Equals((object) value))
        return;
      this.m_state = value;
      this.Invalidate();
    }
  }

  public override Size GetPreferredSize(Size proposedSize)
  {
    Size preferredSize = base.GetPreferredSize(proposedSize);
    return this.m_showSplit && !string.IsNullOrEmpty(this.Text) && TextRenderer.MeasureText(this.Text, this.Font).Width + 14 > preferredSize.Width ? preferredSize + new Size(14 + CUiC3DSplitButton.m_borderSize * 2, 0) : preferredSize;
  }

  protected override bool IsInputKey(Keys keyData)
  {
    return keyData.Equals((object) Keys.Down) && this.m_showSplit || base.IsInputKey(keyData);
  }

  protected override void OnGotFocus(EventArgs e)
  {
    if (!this.m_showSplit)
    {
      base.OnGotFocus(e);
    }
    else
    {
      if (this.State.Equals((object) PushButtonState.Pressed) || this.State.Equals((object) PushButtonState.Disabled))
        return;
      this.State = PushButtonState.Default;
    }
  }

  protected override void OnKeyDown(KeyEventArgs kevent)
  {
    if (this.m_showSplit)
    {
      if (kevent.KeyCode.Equals((object) Keys.Down))
        this.ShowContextMenuStrip();
      else if (kevent.KeyCode.Equals((object) Keys.Space) && kevent.Modifiers == Keys.None)
        this.State = PushButtonState.Pressed;
    }
    base.OnKeyDown(kevent);
  }

  protected override void OnKeyUp(KeyEventArgs kevent)
  {
    if (kevent.KeyCode.Equals((object) Keys.Space) && Control.MouseButtons == MouseButtons.None)
      this.State = PushButtonState.Normal;
    base.OnKeyUp(kevent);
  }

  protected override void OnLostFocus(EventArgs e)
  {
    if (!this.m_showSplit)
    {
      base.OnLostFocus(e);
    }
    else
    {
      if (this.State.Equals((object) PushButtonState.Pressed) || this.State.Equals((object) PushButtonState.Disabled))
        return;
      this.State = PushButtonState.Normal;
    }
  }

  protected override void OnMouseDown(MouseEventArgs e)
  {
    if (!this.m_showSplit)
      base.OnMouseDown(e);
    else if (this.m_dropDownRectangle.Contains(e.Location))
      this.ShowContextMenuStrip();
    else
      this.State = PushButtonState.Pressed;
  }

  protected override void OnMouseEnter(EventArgs e)
  {
    if (!this.m_showSplit)
    {
      base.OnMouseEnter(e);
    }
    else
    {
      if (this.State.Equals((object) PushButtonState.Pressed) || this.State.Equals((object) PushButtonState.Disabled))
        return;
      this.State = PushButtonState.Hot;
    }
  }

  protected override void OnMouseLeave(EventArgs e)
  {
    if (!this.m_showSplit)
    {
      base.OnMouseLeave(e);
    }
    else
    {
      if (this.State.Equals((object) PushButtonState.Pressed) || this.State.Equals((object) PushButtonState.Disabled))
        return;
      if (this.Focused)
        this.State = PushButtonState.Default;
      else
        this.State = PushButtonState.Normal;
    }
  }

  protected override void OnMouseUp(MouseEventArgs mevent)
  {
    if (!this.m_showSplit)
    {
      base.OnMouseUp(mevent);
    }
    else
    {
      if (this.ContextMenuStrip != null && this.ContextMenuStrip.Visible)
        return;
      this.SetButtonDrawState();
      if (!this.Bounds.Contains(this.Parent.PointToClient(Cursor.Position)) || this.m_dropDownRectangle.Contains(mevent.Location))
        return;
      this.OnClick(new EventArgs());
    }
  }

  protected override void OnPaint(PaintEventArgs pevent)
  {
    base.OnPaint(pevent);
    if (!this.m_showSplit)
      return;
    Graphics graphics = pevent.Graphics;
    Rectangle clientRectangle1 = this.ClientRectangle;
    if (this.State != PushButtonState.Pressed && this.IsDefault && !Application.RenderWithVisualStyles)
    {
      Rectangle bounds = clientRectangle1;
      bounds.Inflate(-1, -1);
      ButtonRenderer.DrawButton(graphics, bounds, this.State);
      graphics.DrawRectangle(SystemPens.WindowFrame, 0, 0, clientRectangle1.Width - 1, clientRectangle1.Height - 1);
    }
    else
      ButtonRenderer.DrawButton(graphics, clientRectangle1, this.State);
    this.m_dropDownRectangle = new Rectangle(clientRectangle1.Right - 14 - 1, CUiC3DSplitButton.m_borderSize, 14, clientRectangle1.Height - CUiC3DSplitButton.m_borderSize * 2);
    int borderSize = CUiC3DSplitButton.m_borderSize;
    Rectangle rectangle = new Rectangle(borderSize, borderSize, clientRectangle1.Width - this.m_dropDownRectangle.Width - borderSize, clientRectangle1.Height - borderSize * 2);
    bool flag = this.State == PushButtonState.Hot || this.State == PushButtonState.Pressed || !Application.RenderWithVisualStyles;
    if (this.RightToLeft == RightToLeft.Yes)
    {
      this.m_dropDownRectangle.X = clientRectangle1.Left + 1;
      rectangle.X = this.m_dropDownRectangle.Right;
      if (flag)
      {
        graphics.DrawLine(SystemPens.ButtonShadow, clientRectangle1.Left + 14, CUiC3DSplitButton.m_borderSize, clientRectangle1.Left + 14, clientRectangle1.Bottom - CUiC3DSplitButton.m_borderSize);
        graphics.DrawLine(SystemPens.ButtonFace, clientRectangle1.Left + 14 + 1, CUiC3DSplitButton.m_borderSize, clientRectangle1.Left + 14 + 1, clientRectangle1.Bottom - CUiC3DSplitButton.m_borderSize);
      }
    }
    else if (flag)
    {
      graphics.DrawLine(SystemPens.ButtonShadow, clientRectangle1.Right - 14, CUiC3DSplitButton.m_borderSize, clientRectangle1.Right - 14, clientRectangle1.Bottom - CUiC3DSplitButton.m_borderSize);
      graphics.DrawLine(SystemPens.ButtonFace, clientRectangle1.Right - 14 - 1, CUiC3DSplitButton.m_borderSize, clientRectangle1.Right - 14 - 1, clientRectangle1.Bottom - CUiC3DSplitButton.m_borderSize);
    }
    this.PaintArrow(graphics, this.m_dropDownRectangle);
    Graphics g = graphics;
    Rectangle clientRectangle2 = this.ClientRectangle;
    int width = clientRectangle2.Width - 14;
    clientRectangle2 = this.ClientRectangle;
    int height = clientRectangle2.Height;
    Rectangle bounds1 = new Rectangle(0, 0, width, height);
    this.PaintTextandImage(g, bounds1);
    if (this.State == PushButtonState.Pressed || !this.Focused)
      return;
    ControlPaint.DrawFocusRectangle(graphics, rectangle);
  }

  private void PaintTextandImage(Graphics g, Rectangle bounds)
  {
    Rectangle textRectangle;
    Rectangle imageRectangle;
    this.CalculateButtonTextAndImageLayout(ref bounds, out textRectangle, out imageRectangle);
    if (this.Image != null)
    {
      if (this.Enabled)
        g.DrawImage(this.Image, imageRectangle.X, imageRectangle.Y, this.Image.Width, this.Image.Height);
      else
        ControlPaint.DrawImageDisabled(g, this.Image, imageRectangle.X, imageRectangle.Y, this.BackColor);
    }
    if (!this.UseMnemonic)
      this.m_textFormatFlags |= TextFormatFlags.NoPrefix;
    else if (!this.ShowKeyboardCues)
      this.m_textFormatFlags |= TextFormatFlags.HidePrefix;
    if (string.IsNullOrEmpty(this.Text))
      return;
    if (this.Enabled)
      TextRenderer.DrawText((IDeviceContext) g, this.Text, this.Font, textRectangle, SystemColors.ControlText, this.m_textFormatFlags);
    else
      ControlPaint.DrawStringDisabled((IDeviceContext) g, this.Text, this.Font, this.BackColor, textRectangle, this.m_textFormatFlags);
  }

  private void CalculateButtonTextAndImageLayout(
    ref Rectangle content_rect,
    out Rectangle textRectangle,
    out Rectangle imageRectangle)
  {
    Size sizeOfObject1 = TextRenderer.MeasureText(this.Text, this.Font, content_rect.Size, this.m_textFormatFlags);
    Size sizeOfObject2 = this.Image == null ? Size.Empty : this.Image.Size;
    textRectangle = Rectangle.Empty;
    imageRectangle = Rectangle.Empty;
    switch (this.TextImageRelation)
    {
      case TextImageRelation.Overlay:
        textRectangle = CUiC3DSplitButton.OverlayObjectRect(ref content_rect, ref sizeOfObject1, this.TextAlign);
        if (this.m_state == PushButtonState.Pressed && !Application.RenderWithVisualStyles)
          textRectangle.Offset(1, 1);
        if (this.Image == null)
          break;
        imageRectangle = CUiC3DSplitButton.OverlayObjectRect(ref content_rect, ref sizeOfObject2, this.ImageAlign);
        break;
    }
  }

  private static Rectangle OverlayObjectRect(
    ref Rectangle container,
    ref Size sizeOfObject,
    System.Drawing.ContentAlignment alignment)
  {
    int x;
    int y;
    switch (alignment)
    {
      case System.Drawing.ContentAlignment.TopLeft:
        x = 4;
        y = 4;
        break;
      case System.Drawing.ContentAlignment.TopCenter:
        x = (container.Width - sizeOfObject.Width) / 2;
        y = 4;
        break;
      case System.Drawing.ContentAlignment.TopRight:
        x = container.Width - sizeOfObject.Width - 4;
        y = 4;
        break;
      case System.Drawing.ContentAlignment.MiddleLeft:
        x = 4;
        y = (container.Height - sizeOfObject.Height) / 2;
        break;
      case System.Drawing.ContentAlignment.MiddleCenter:
        x = (container.Width - sizeOfObject.Width) / 2;
        y = (container.Height - sizeOfObject.Height) / 2;
        break;
      case System.Drawing.ContentAlignment.MiddleRight:
        x = container.Width - sizeOfObject.Width - 4;
        y = (container.Height - sizeOfObject.Height) / 2;
        break;
      case System.Drawing.ContentAlignment.BottomLeft:
        x = 4;
        y = container.Height - sizeOfObject.Height - 4;
        break;
      case System.Drawing.ContentAlignment.BottomCenter:
        x = (container.Width - sizeOfObject.Width) / 2;
        y = container.Height - sizeOfObject.Height - 4;
        break;
      case System.Drawing.ContentAlignment.BottomRight:
        x = container.Width - sizeOfObject.Width - 4;
        y = container.Height - sizeOfObject.Height - 4;
        break;
      default:
        x = 4;
        y = 4;
        break;
    }
    return new Rectangle(x, y, sizeOfObject.Width, sizeOfObject.Height);
  }

  private void PaintArrow(Graphics g, Rectangle dropDownRect)
  {
    Point point = new Point(Convert.ToInt32(dropDownRect.Left + dropDownRect.Width / 2), Convert.ToInt32(dropDownRect.Top + dropDownRect.Height / 2));
    point.X += dropDownRect.Width % 2;
    Point[] points = new Point[3]
    {
      new Point(point.X - 2, point.Y - 1),
      new Point(point.X + 3, point.Y - 1),
      new Point(point.X, point.Y + 2)
    };
    g.FillPolygon(SystemBrushes.ControlText, points);
  }

  private void ShowContextMenuStrip()
  {
    if (this.m_skipNextOpen)
    {
      this.m_skipNextOpen = false;
    }
    else
    {
      this.State = PushButtonState.Pressed;
      if (this.ContextMenuStrip == null)
        return;
      this.ContextMenuStrip.Closing += new ToolStripDropDownClosingEventHandler(this.ContextMenuStrip_Closing);
      this.ContextMenuStrip.Show((Control) this, new Point(0, this.Height), ToolStripDropDownDirection.BelowRight);
    }
  }

  private void ContextMenuStrip_Closing(object sender, ToolStripDropDownClosingEventArgs e)
  {
    if (sender is ContextMenuStrip contextMenuStrip)
      contextMenuStrip.Closing -= new ToolStripDropDownClosingEventHandler(this.ContextMenuStrip_Closing);
    this.SetButtonDrawState();
    if (e.CloseReason != ToolStripDropDownCloseReason.AppClicked)
      return;
    this.m_skipNextOpen = this.m_dropDownRectangle.Contains(this.PointToClient(Cursor.Position));
  }

  private void SetButtonDrawState()
  {
    if (this.Bounds.Contains(this.Parent.PointToClient(Cursor.Position)))
      this.State = PushButtonState.Hot;
    else if (this.Focused)
      this.State = PushButtonState.Default;
    else
      this.State = PushButtonState.Normal;
  }
}
